import sys
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QPushButton
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from PyQt5.QtCore import QTimer
 
 
class MyWidget(QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.init_ui()
 
    def init_ui(self):
        self.resize(1000, 500)
        self.setWindowTitle("动态数据显示")
 
        # 解决无法显示中文
        plt.rcParams['font.sans-serif'] = ['SimHei']
        # 解决无法显示负号
        plt.rcParams['axes.unicode_minus'] = False
 
        # 设置画布部分
        self.fig = plt.figure()
        self.canvas = FC(self.fig)
 
        # 设置布局，将组件添加到布局中
        self.layout = QVBoxLayout()
 
        self.layout.addWidget(self.canvas)
 
        self.setLayout(self.layout)
        
        # 设置定时器，用于更新
        self.timer1 = QTimer(self)
        self.timer1.timeout.connect(self.update_plot)
        self.timer1.start(100)
 
        self.x = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021']  # x轴
        self.y = [0,1,2,3,4,5,6,7,8,9,10,11]  # y轴
 
    def update_plot(self):
        for i in range(len(self.y)-1):
            firstNumber = self.y[0]
            self.y[i] = self.y[i+1]
            self.y[-1] = firstNumber
        plt.cla()  # 清空画布
        plt.plot(self.x, self.y, color='blue', linewidth=1.0, linestyle='-', marker='o')
        plt.xlabel("年份")
        plt.ylabel("数量")
        self.canvas.draw()  # 绘制
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    m = MyWidget()
    m.show()
    sys.exit(app.exec_())
 